package com.deuwise.job.config;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.deuwise.common.utils.SpringContextHolder;
import com.deuwise.job.entity.TaskDo;
import com.deuwise.job.service.TaskDoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.List;

/**
 *
 * @Author:chenwenchao
 * @Date:2018/5/07 Description:
 * email 448012005@qq.com
 *
 * 启动数据库中已经设定为 启动状态(status:true)的任务 项目启动时init
 */
@Configuration
public class DataSourceJobThread extends Thread {

  private static final Logger log = LoggerFactory.getLogger(DataSourceJobThread.class);

  @Autowired
  TaskDoService jobService;

  @Override
  public void run() {
    try {
      Thread.sleep(1000);
      log.info("---------线程启动---------");
      JobTask jobTask = SpringContextHolder.getBean("jobTask");
        Wrapper<TaskDo> taskDoWrapper=new EntityWrapper<>();
        taskDoWrapper.eq("job_status",1);
      List<TaskDo> jobList = jobService.selectList(taskDoWrapper);
      //开启任务
     jobList.forEach(jobs -> {
       log.info("---任务["+jobs.getId()+"]系统 init--开始启动---------");
        jobTask.startJob(jobs);
          }
      );
     if(jobList.size()==0){
       log.info("---数据库暂无启动的任务---------");
     }else {
       System.out.println("---任务启动完毕---------");
     }
   } catch (InterruptedException e) {
     e.printStackTrace();
  }
  }
}
